package com.quartz.dao;

import com.quartz.entity.JobAndTrigger;
import com.quartz.util.MapUtil;
import org.quartz.utils.DBConnectionManager;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: zyj
 * @create: 2020-01-02 11:29
 **/
@Component
public class MyQuartzDao{

    /**
    * @Description: 获取定时任务列表
    * @Param: [dataSourceName]
    * @return: java.util.List<com.quartz.entity.JobAndTrigger>
    * @Author: zyj
    * @Date: 2020/5/26
    */
    public List<JobAndTrigger> jobAndTriggerList(String dataSourceName) {
        List<JobAndTrigger> list = new ArrayList<>();
        Map<String, Object> rsOrgMap = null;

        Connection connection = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            String sql = "SELECT " +
                    " qrtz_job_details.JOB_NAME  jobName, " +
                    " qrtz_job_details.JOB_GROUP AS jobGroup, " +
                    " qrtz_job_details.JOB_CLASS_NAME AS jobClassName, " +
                    " qrtz_job_details.DESCRIPTION AS description, " +
                    " qrtz_triggers.TRIGGER_NAME AS triggerName, " +
                    " qrtz_triggers.TRIGGER_GROUP AS triggerGroup, " +
                    " qrtz_cron_triggers.CRON_EXPRESSION AS cronExpression, " +
                    " qrtz_cron_triggers.TIME_ZONE_ID AS timeZoneId, " +
                    " qrtz_triggers.TRIGGER_STATE AS triggerState " +
                    " FROM " +
                    " qrtz_job_details " +
                    " JOIN qrtz_triggers " +
                    " JOIN qrtz_cron_triggers ON qrtz_job_details.JOB_NAME = qrtz_triggers.JOB_NAME " +
                    " AND qrtz_triggers.TRIGGER_NAME = qrtz_cron_triggers.TRIGGER_NAME " +
                    " AND qrtz_triggers.TRIGGER_GROUP = qrtz_cron_triggers.TRIGGER_GROUP ";

            connection = DBConnectionManager.getInstance().getConnection(dataSourceName);
            stmt = connection.prepareStatement(sql);
            rs = stmt.executeQuery();
            ResultSetMetaData rsmdOrg = rs.getMetaData();
            int orgColumns = rsmdOrg.getColumnCount();
            while (rs.next()) {
                rsOrgMap = new HashMap<String, Object>(orgColumns);
                for (int j = 1; j < orgColumns + 1; j++) {
                    rsOrgMap.put(rsmdOrg.getColumnName(j), rs.getObject(j));
                }
                JobAndTrigger jobAndTrigger = MapUtil.mapToBean(rsOrgMap, JobAndTrigger.class);
                list.add(jobAndTrigger);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if(null!=connection){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return list;

    }

}
